﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;
using WMSSoft.Lib.WinKStart.ViewModel;

namespace WMSSoft.WinKStart.ViewModel
{
    public class Settings_NewContextMenuItemViewModel : CCommandViewModel
    {
        cWinKStartContextMenuItem _Data = null;
        List<cRegion> _Regions = null;
        KeyValueAssignment _SelectedItem = null;
        bool _CanDeleteItem = false;
        List<KeyValueAssignment> _UsedRegionCaptions = null;
       

        public Settings_NewContextMenuItemViewModel(List<cRegion> Regions) 
        {
            ViewName = LanguageStrings.GetText("New Context-Menu-Item");
            _Data = new cWinKStartContextMenuItem();
            _UsedRegionCaptions = new List<KeyValueAssignment>();
            _Regions = Regions;
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Add"), new CDelegatingCommand(Delegate => this.Cmd_AddItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Delete"), new CDelegatingCommand(Delegate => this.Cmd_DeleteItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Apply"), new CDelegatingCommand(Delegate => this.Cmd_ApplyChanges())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Discard"), new CDelegatingCommand(Delegate => this.CMd_DiscardChanges())));
        }

        public Settings_NewContextMenuItemViewModel(cWinKStartContextMenuItem Data,
                                                    List<cRegion> Regions)
        {
            ViewName = LanguageStrings.GetText("Edit Context-Menu-Item");
            _Data = Data;
            _UsedRegionCaptions = new List<KeyValueAssignment>();
            ApplyUsedRegionsToUsedRegionCaptions();
            _Regions = Regions;
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Add"), new CDelegatingCommand(Delegate => this.Cmd_AddItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Delete"), new CDelegatingCommand(Delegate => this.Cmd_DeleteItem())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Apply"), new CDelegatingCommand(Delegate => this.Cmd_ApplyChanges())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Discard"), new CDelegatingCommand(Delegate => this.CMd_DiscardChanges())));
            PropertyChanged += Settings_NewContextMenuItemViewModel_PropertyChanged;
        }

        void Settings_NewContextMenuItemViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            try
            {
                if (e.PropertyName == "SelectedItem")
                    CanDeleteItem = true;

            }
            catch (Exception Ex)
            {

            }
        }

        #region Properties

        public cWinKStartContextMenuItem Data
        {
            get { return _Data; }
        }

        public CSingleCommandViewModel Cmd_Add
        {
            get { return m_oCommands[0]; }
        }

        public CSingleCommandViewModel Cmd_Delete
        {
            get { return m_oCommands[1]; }
        }

        public CSingleCommandViewModel Cmd_Apply
        {
            get { return m_oCommands[2]; }
        }

        public CSingleCommandViewModel Cmd_Discard
        {
            get { return m_oCommands[3]; }
        }

        public List<KeyValueAssignment> RegionsListCaptions
        {
            get { return _UsedRegionCaptions; }
            set
            {
                _UsedRegionCaptions = value;
                OnPropertyChanged("RegionsListCaptions");
            }
        }

        public KeyValueAssignment SelectedItem
        {
            get { return _SelectedItem; }
            set
            {
                _SelectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }

        public bool CanDeleteItem
        {
            get { return _CanDeleteItem; }
            set
            {
                _CanDeleteItem = value;
                OnPropertyChanged("CanDeleteItem");
            }
        }

        #endregion

        #region Command Procedures

        void Cmd_AddItem()
        {
            try
            {
                System.Windows.Controls.ListBox tmpListBox = null;
                if ((System.Windows.Controls.ListBox)((CDelegatingCommand)Cmd_Add.Command).CommandParameter != null)
                {
                    tmpListBox = (System.Windows.Controls.ListBox)((CDelegatingCommand)Cmd_Add.Command).CommandParameter;

                    Setting_SelectRegionViewModel tmpViewModel = new Setting_SelectRegionViewModel(_Regions);

                    Settings_SelectRegion SelRegionDlg = new Settings_SelectRegion();
                    SelRegionDlg.DataContext = tmpViewModel;
                    if (SelRegionDlg.ShowDialog() == true)
                    {
                        string selection = ((Setting_SelectRegionViewModel)SelRegionDlg.DataContext).SelectedRegion;

                        if (ContainsItem(selection) == false)
                        {
                            KeyValueAssignment NewItem = new KeyValueAssignment(selection, GetCaptionFromKey(selection));
                            _UsedRegionCaptions.Add(NewItem);
                            //OnPropertyChanged("RegionsListCaptions");
                            tmpListBox.Items.Refresh();
                            //OnPropertyChanged("UsedIn");
                            //OnPropertyChanged("ItemsSource");
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_ADD_USED_CONTEXTMENU_REGION, Ex, EMessageTypes.Error);
            }
        }

        void Cmd_DeleteItem()
        {
            try
            {
                System.Windows.Controls.ListBox tmpListBox = null;
                if ((System.Windows.Controls.ListBox)((CDelegatingCommand)Cmd_Delete.Command).CommandParameter != null)
                {
                    tmpListBox = (System.Windows.Controls.ListBox)((CDelegatingCommand)Cmd_Delete.Command).CommandParameter;

                    //_SelectedItem

                    if (_SelectedItem != null && ContainsItem(_SelectedItem.Key) == true)
                    {                        
                        CanDeleteItem = false;
                        int itmpIndex = GetIndexFromKey(_SelectedItem.Key);
                        if (itmpIndex > -1)
                        {
                            if (System.Windows.MessageBox.Show(LanguageStrings.GetText("Delete selected Region?"),
                            LanguageStrings.GetText("Question"),
                            System.Windows.MessageBoxButton.YesNo) == System.Windows.MessageBoxResult.Yes)
                            {
                                _UsedRegionCaptions.RemoveAt(itmpIndex);
                                //OnPropertyChanged("RegionsListCaptions");
                                tmpListBox.Items.Refresh();
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_DELETE_USED_CONTEXTMENU_REGION, Ex, true, EMessageTypes.Error);
            }
        }

        void Cmd_ApplyChanges()
        {
            try
            {
                Settings_NewContextMenuItem ParentWindow = null;
                if ((Settings_NewContextMenuItem)((CDelegatingCommand)Cmd_Apply.Command).CommandParameter != null)
                {
                    ParentWindow = (Settings_NewContextMenuItem)((CDelegatingCommand)Cmd_Apply.Command).CommandParameter;

                    if (ParentWindow != null)
                    {
                        _Data.UsedIn.Clear();
                        //Apply DataChanges from ChildUse to Object
                        foreach (KeyValueAssignment Item in _UsedRegionCaptions)
                        {
                            _Data.UsedIn.Add(Item.Key);
                        }

                        ParentWindow.DialogResult = true;
                        ParentWindow.Close();
                    }
                }
            }
            catch (Exception Ex) 
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_APPLY_SETTINGS, Ex, EMessageTypes.Error);
            }
        }

        void CMd_DiscardChanges()
        {
            try
            {
                Settings_NewContextMenuItem ParentWindow = null;
                if ((Settings_NewContextMenuItem)((CDelegatingCommand)Cmd_Discard.Command).CommandParameter != null)
                {
                    ParentWindow = (Settings_NewContextMenuItem)((CDelegatingCommand)Cmd_Discard.Command).CommandParameter;

                    if (ParentWindow != null)
                        ParentWindow.Close();
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_DISCARD_SETTINGS, Ex, EMessageTypes.Error);
            }
        }

        #endregion

        #region Helper Methods

        bool ContainsItem(string Name)
        {
            try
            {
                foreach (KeyValueAssignment Item in _UsedRegionCaptions)
                {
                    if (Item.Key == Name)
                        return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        string GetCaptionFromKey(string Key)
        {
            if (cMenuConfigReader.TabPages.ContainsKey(Key))
            {
                return LanguageStrings.GetText(cMenuConfigReader.TabPages[Key].Caption);
            } else 
                return "";
        }

        int GetIndexFromKey(string Key)
        {
            for (int iCounter = 0; iCounter < _UsedRegionCaptions.Count; iCounter++)
            {
                if (_UsedRegionCaptions[iCounter].Key == Key)
                {
                    return iCounter;
                }
            }
            return -1;
        }

        void ApplyUsedRegionsToUsedRegionCaptions()
        {
            try
            {
                if (_Data != null && _Data.UsedIn != null && _Data.UsedIn.Count > 0)
                {
                    foreach (string Item in _Data.UsedIn)
                    {                        
                        if (cMenuConfigReader.TabPages.ContainsKey(Item) == true)
                        {
                            KeyValueAssignment tmpValue = new KeyValueAssignment(Item, LanguageStrings.GetText(cMenuConfigReader.TabPages[Item].Caption));
                            _UsedRegionCaptions.Add(tmpValue);
                        }
                    }
                }
            }
            catch (Exception Ex)
            {

            }

        }

        #endregion
    }
}
